package com.example.permission.mapper;

import com.example.permission.entity.Permission;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author qwer3
 * @version 1.0.0
 * @create 2025/11/1 22:34
 */
@Mapper
public interface SystemPermissionMapper {

    @Insert("""
INSERT INTO sys_permission (permission_id, parent_id, sys, path, name, redirect) 
VALUES (#{permissionId}, #{parentId}, #{sys}, #{path}, #{name}, #{redirect});
""")
    void addPermission(Permission permission);

    @Select("""
<script>
select * from sys_permission
</script>
""")
    List<Permission> getSystemPermission();

    @Select("""
<script>
select * 
from sys_role_permission rp 
    left join sys_permission p on rp.permission_id = p.permission_id
where rp.role_id = #{roleId}   
  <if test="system != null and system != ''">
   and sys = #{system}
  </if>
</script>
""")
    List<Permission> getSystemPermissionByParam(@Param("roleId") String roleId,
                                         @Param("system") String system);

    @Select("""
select * from sys_permission where parent_id = 'sys_root_default';
""")
    List<Permission> getSystemRoot();
}
